﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cocos2d;
using Meteorite;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Input;
using CocosDenshion;

namespace Meteorite
{
    public class MainMenuScene : CCScene
    {
        CCLabelBMFont Title;
        CCMenuItemLabel ButtonPlay;
        CCMenuItemLabel ButtonSound;
        CCMenuItemLabel ButtonAbout;
        CCMenuItemLabel ButtonExit;

        CCMenuItemSprite ButtonXLive;

        Vector3 cameraPosBackup;
        const float radius = 30.0f;
        const float radian = MathHelper.Pi / 90;

        int nTick = 0;
        public MainMenuScene()
        {
            CCSize size = CCDirector.sharedDirector().getWinSize();

            //Title
            Title = CCLabelBMFont.labelWithString(GameConstant.MainMenuScene_Title, GameConstant.Font_Futura_48);
            addChild(Title);
            Title.position = new CCPoint(size.width / 2, size.height);
            Title.scale = 2.0f;

            //Menu
            CCLabelBMFont LabelPlay = CCLabelBMFont.labelWithString(GameConstant.MainMenuScene_Btn_Play, GameConstant.Font_Futura_48);
            ButtonPlay = CCMenuItemLabel.itemWithLabel(LabelPlay, ButtonPlay, this.btnCallback);
            ButtonPlay.position = new CCPoint(size.width / 2, -size.height);

            CCLabelBMFont LabelSetting = CCLabelBMFont.labelWithString(GameConstant.MainMenuScene_Btn_SoundOn, GameConstant.Font_Futura_48);
            ButtonSound = CCMenuItemLabel.itemWithLabel(LabelSetting, ButtonSound, this.btnCallback);
            ButtonSound.position = new CCPoint(size.width / 2, -size.height);

            CCLabelBMFont LabelAbout = CCLabelBMFont.labelWithString(GameConstant.MainMenuScene_Btn_About, GameConstant.Font_Futura_48);
            ButtonAbout = CCMenuItemLabel.itemWithLabel(LabelAbout, ButtonAbout, this.btnCallback);
            ButtonAbout.position = new CCPoint(size.width / 2, -size.height);

            CCLabelBMFont LabelExit = CCLabelBMFont.labelWithString(GameConstant.MainMenuScene_Btn_Exit, GameConstant.Font_Futura_48);
            ButtonExit = CCMenuItemLabel.itemWithLabel(LabelExit, ButtonExit, this.btnCallback);
            ButtonExit.position = new CCPoint(size.width / 2, -size.height);


            CCTexture2D texture0 = CCTextureCache.sharedTextureCache().addImage(GameConstant.Texture_XLive_Btn);
            CCTexture2D texture1 = CCTextureCache.sharedTextureCache().addImage(GameConstant.Texture_XLive_A);
                

            // manually add frames to the frame cache
            CCSpriteFrame frame0 = CCSpriteFrame.frameWithTexture(texture0, new CCRect( 0, 0, 64, 64));
            CCSpriteFrame frame1 = CCSpriteFrame.frameWithTexture(texture1, new CCRect(1, 1, 64, 64)); //这要是填CCRect( 0, 0, 64, 64)就有问题
            CCSprite NormalSprite = CCSprite.spriteWithSpriteFrame(frame0);
            CCSprite SelectSprite = CCSprite.spriteWithFile(GameConstant.Texture_XLive_S_Btn);
            List<CCSpriteFrame> animFrames = new List<CCSpriteFrame>();
            animFrames.Add(frame0);
            animFrames.Add(frame1);
            CCAnimation animation = CCAnimation.animationWithFrames(animFrames, 0.8f);
            CCAnimate animate = CCAnimate.actionWithAnimation(animation, false);
            NormalSprite.runAction(CCRepeatForever.actionWithAction(animate));
                
            ButtonXLive = CCMenuItemImage.itemFromNormalImage(GameConstant.Texture_XLive_Btn, GameConstant.Texture_XLive_Btn, this, this.btnCallback);
            ButtonXLive = CCMenuItemSprite.itemFromNormalSprite(NormalSprite, SelectSprite, this, this.btnCallback);
            ButtonXLive.position = new CCPoint(size.width - 32, 32);

            //Menu
            CCMenu pMenu = CCMenu.menuWithItems(ButtonPlay, ButtonSound, ButtonAbout, ButtonExit, ButtonXLive);
            pMenu.position = new CCPoint(0, 0);
            addChild(pMenu);

            MenuGetIn();

            schedule(Tick, 0.03f);

            cameraPosBackup = Meteorite.camera.Position;
            cameraPosBackup.X -= radius;
            Meteorite.camera.StopShake();

            OnSoundConfigChanged();
        }

        public override void onEnter()
        {
            base.onEnter();
            Meteorite.AdMgr.SetVisible(true);
        }
        public override void onExit()
        {
            base.onExit();
            Meteorite.AdMgr.SetVisible(false);
        }
        public void OnSoundConfigChanged()
        {
            if (0 == UserData.Instance().GetValue(GameConstant.UserData_Sound))
            {
                SimpleAudioEngine.sharedEngine().playBackgroundMusic(CCFileUtils.fullPathFromRelativePath(GameConstant.Sound_Menu_Bkg), true);
                ButtonSound.setString(GameConstant.MainMenuScene_Btn_SoundOff);
            }
            else
            {
                SimpleAudioEngine.sharedEngine().stopBackgroundMusic();
                ButtonSound.setString(GameConstant.MainMenuScene_Btn_SoundOn);
            }
        }
        private void Tick(float dt)
        {
            nTick++;
  
            Vector3 Pos = cameraPosBackup;
            Pos.X += (float)Math.Cos(nTick * radian) * radius;
            Pos.Y += (float)Math.Sin(nTick * radian) * radius;
            Meteorite.camera.ChasePosition = Pos;
            if( Meteorite.ship.Valid )
            {
                if (Meteorite.camera.Position.Z - Meteorite.ship.Position.Z >= GameConstant.CameraMaxDistance-1)
                {
                    Meteorite.ship.Valid = false;
                }
            }
        }
        private void MenuGetIn()
        {
            CCSize size = CCDirector.sharedDirector().getWinSize();
            Title.runAction(CCMoveTo.actionWithDuration(0.8f, new CCPoint(size.width / 2, size.height - 130)));

            ButtonPlay.runAction(CCMoveTo.actionWithDuration(0.5f, new CCPoint(size.width / 2, size.height - 250)));

            ButtonSound.runAction(CCMoveTo.actionWithDuration(0.6f, new CCPoint(size.width / 2, size.height - 300)));

            ButtonAbout.runAction(CCMoveTo.actionWithDuration(0.7f, new CCPoint(size.width / 2, size.height - 350)));

            ButtonExit.runAction(CCMoveTo.actionWithDuration(0.7f, new CCPoint(size.width / 2, size.height - 400)));
        }
        private void MenuGetOut()
        {
            CCSize size = CCDirector.sharedDirector().getWinSize();
            Title.runAction(CCMoveTo.actionWithDuration(0.8f, new CCPoint(size.width / 2, size.height+60)));

            ButtonPlay.runAction(CCMoveTo.actionWithDuration(0.8f, new CCPoint(size.width / 2, -size.height)));

            ButtonSound.runAction(CCMoveTo.actionWithDuration(0.5f, new CCPoint(size.width / 2, -size.height)));

            ButtonAbout.runAction(CCMoveTo.actionWithDuration(0.2f, new CCPoint(size.width / 2, -size.height)));
            ButtonExit.runAction(CCMoveTo.actionWithDuration(0.1f, new CCPoint(size.width / 2, -size.height)));

        }
        public void callback()
        {
            CCScene pScene = new GameScene();
            CCDirector.sharedDirector().replaceScene(pScene);
        }
        public void btnCallback(CCObject pSender)
        {
            if (pSender == ButtonPlay)
            {
                MenuGetOut();
                runAction(CCSequence.actions(CCDelayTime.actionWithDuration(1.0f), CCCallFunc.actionWithTarget(this, new SEL_CallFunc(callback))));
            }
            else if (pSender == ButtonSound)
            {
                int flag = UserData.Instance().GetValue(GameConstant.UserData_Sound);
                UserData.Instance().SetValue(GameConstant.UserData_Sound, 1 == flag ? 0 : 1);
                OnSoundConfigChanged();
            }
            else if (pSender == ButtonAbout)
            {
                //MenuGetOut();
                CCDirector.sharedDirector().pushScene(new StatsticsScene());
                
                //runAction(CCSequence.actions(CCDelayTime.actionWithDuration(1.0f), CCCallFunc.actionWithTarget(this, new SEL_CallFunc(callback))));
            }
            else if (pSender == ButtonExit)
            {
                UserData.Instance().Save();

                CCDirector.sharedDirector().end();
                CCApplication.sharedApplication().Game.Exit();
            }
            else if (pSender == ButtonXLive)
            {
                //XLive
                XliveStartFormEx liveform = new XliveStartFormEx(Meteorite.livemanager);
                liveform.Background = Meteorite.liveBkg;
                liveform.BackgroundAnimation = true;
                liveform.AnimationSpeed = 5;
                liveform.NewgameButton = false;
                liveform.ContinueButton = false;
                liveform.ExitButton = false;

                liveform.Show();
                Meteorite.AdMgr.SetVisible(false);
            }
            if (0 == UserData.Instance().GetValue(GameConstant.UserData_Sound))
            {
                SimpleAudioEngine.sharedEngine().playEffect(CCFileUtils.fullPathFromRelativePath(GameConstant.Sound_Btn));
            }
        }

    }
}
